grammar test;

options {
	language=CSharp2; 
// 	language=Java;
}


@header
{
	using System.Collections.Generic;
}
@parser::members 
{

	protected override Object RecoverFromMismatchedToken(IIntStream input, int ttype, BitSet follow){
		throw new MismatchedTokenException(); return null;
	}

	public override Object RecoverFromMismatchedSet(IIntStream input, RecognitionException e, BitSet follow){
		throw new MismatchedSetException(); return null;
	}
}

@rulecatch {
catch (RecognitionException e) {
	throw e;
}
}



query	returns[String result]:
	name=dang1 {
			if (name!=null) result = "SELECT * FROM book WHERE `_ten_sach` LIKE '\%" + name + "\%'" ; else result=null;
		}
	|
	name=dang2 {
		if (name!=null) result = "SELECT * FROM book WHERE `_tac_gia` LIKE '\%" + name + "\%'" ; else result=null;
	}
	
	|
	name=dang3 {
		if (name!=null) result = "SELECT * FROM book WHERE `_ten_sach` LIKE '\%" + name + "\%'" ; else result=null;
	}
	
		|
	name=dang4 {
		if (name!=null) result = "SELECT * FROM book WHERE `nam` " + name  ; else result=null;
	}
	
		|
	name=dang5 {
		if (name!=null) result = "SELECT * FROM book WHERE `_ten_sach` LIKE '\%" + name + "\%'" ; else result=null;
	}
		
;

//sach abcxyz viet bang ngon ngu gi
dang5 returns[String result]: dang1o a=str ' ' 'duoc '? Write 'bang ' ('tieng ' | 'ngon ngu ') ('nao' | 'gi')
	{result = a;}
	;

//cac sach xuat ban trong nam abcxyz
dang4 returns[String result]:dang2o Publish a=dang4dt  {result = a;};
dang4dt	returns [String result]
	:	('truoc nam ' a=ID {result= "<" + $a.text;})
	   |	('trong nam ' a=ID {result= "=" + $a.text;})
	   |	('sau nam ' a=ID {result= ">" + $a.text;})
	;

//ai xuat ban sach abcxyz
dang3 returns[String result]	: dang3s dang1o? bookName=str {$result=bookName ;};	
dang3s	:	((Publisher 'nao ') ('da ' )? Publish) ;


//tac gia abcxyz viet sach gi
dang2 returns[String result]:a=dang2s  dang2o ('nao' | 'gi')  {result = a;};
dang2s	returns [String result]
	:	Author authorName=str ' ' ('da ')? Write {result=authorName;};
dang2o	:	Plural?  ('tac pham ' | ( Book) );

//ai viet sach abcxyz
dang1 returns[String result]	: dang1s dang1o? bookName=str {$result=bookName ;};	
dang1s	:	(Who ('da ' )? Write) ;
dang1o : 'tac pham ' | ( Book) ;


 Plural 	:	'nhung ' | 'cac ';
// LuongTuSach	:	('cuon ' | 'bo ' | 'quyen ' | 'tap ')?;
 Book	:	('cuon ' | 'bo ' | 'quyen ')? ('sach ' | 'truyen ' | 'tieu thuyet ' );
 Who	:	'ai ' | (('nguoi ' | Author ) 'nao ');
 Write	:	'viet ' | 'sang tac ' ;
 Author	:	'tac gia ' | 'nha van ' | 'soan gia ';
 Publish	:	'xuat ban ' | 'phat hanh ';
 Publisher
	:	'cong ty ' | 'nha xuat ban ' | 'nha phat hanh ';

str  returns[String result]:   a=ID {String r ; r =$a.text;} (' ' b=ID { r+= " " + $b.text;} )* {$result=r;} ;

//str  returns[String result]:   	a=(ID | Plural | LuongTuSach | Book | Who | Write | Author | Publish | Publisher) {String r ; r =$a.text;} 	(' ' b=ID { r+= " " + $b.text;} )* {$result=r;} ;
//string 	:	  r = ID (' ' ID)*   ;

ID  :   ('a'..'z'|'A'..'Z'|'0'..'9')+;

//INT :   '0'..'9'+ ;

WS  :   ('\t'|'\n')+ {$channel=HIDDEN;}
    ;
